<?php
namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Db;

class Login extends Controller
{
    public function login()
    {
	    if (session('id') && session('account')) {
		    $this->redirect(url('/admin/index/index'));
	    }else{
		    if (request()->isPost()) {
			    $input = request()->param();

			    $token = $input['__token__'];
			    $account = $input['account'];
			    $password = $input['password'];
			    $code = $input['code'];

			    if(checkToken($token)){
				    if(empty($account)) adminReturn(2,'请输入账号');
				    if(empty($password)) adminReturn(2,'请输入密码');
				    if(empty($code)) adminReturn(2,'请输入验证码');
				    if(!captcha_check($code)) adminReturn(2,'验证码错误');

				    $where['account'] = $account;
				    $admin = Db::name('user')->where($where)->find();
				    if ($admin) {
					    // 检查密码
					    if (thinkEncrypt($password) != $admin['password']) adminReturn(2, '密码错误');
					    // 检查状态
					    if ($admin['status'] == 2) adminReturn(2, '用户被禁用，请联系管理员');

					    // 登录成功，记录session
					    session('id', $admin['id']);
					    session('account', $admin['account']);

					    adminReturn(1,'登录成功',url('/admin/index/index'));
				    } else {
					    adminReturn(2,'登录失败');
				    }
			    }else{
				    adminReturn(2, '系统繁忙，请稍后再试~');
			    }
		    }else{
			    return $this->fetch();
		    }
	    }
    }
}
